<?php

class re_dom_element //TODO: add an iterator for the children.
{	
	protected $idPrepend = 'rephp-element-';
	
	protected $tag;
	protected $allowedTags = array(); //div, input, body, html, etc
	protected $allowedAttributes = array();
	protected $allowedChildrenTags = array();
	protected $allowChildren = true;
	protected $quotes = '"';
	protected $innerHtml;
	protected $attributes = array();
	protected $children = array();
	protected $class = array(); //TODO: move this inside attrbutes[] ?
	
	static public $lastId = 1;

	public function __construct()
	{
		$this->id = $this->idPrepend . self::$lastId++;
		return $this;
	}
	
	public function setHtml($html)
	{
		$this->innerHtml = $html;
		return $this;
	}
	
	public function getHtml()
	{
		return $this->innerHtml;
	}
	
	public function __set($name, $value=null)
	{
		if(in_array($name, $this->allowedAttributes))
		{
			$this->attributes[$name] = $value;
		}
		return $this;
	}
	
	public function __get($name)
	{
		if(in_array($name, $this->allowedAttributes))
		{
			if(isset($this->attributes[$name]))
			{
				return $this->attributes[$name];
			}
			else
			{
				return null;
			}
		}
		
		return false;
	}
	
	public function __call($method, $arguments)
	{
		$threeFirst = substr($method, 0, 3);
		if($threeFirst=='get')
		{
			$attribute = substr($method, 3, strlen($method)-3);
			$attribute = strtolower($attribute);
			return $this->{$attribute};
		}
		else if($threeFirst=='set')
		{
			$attribute = substr($method, 3, strlen($method)-3);
			$attribute = strtolower($attribute);
			$this->{$attribute} = $arguments[0];
			
			return $this;
		}
	}
	
	public function addChild($element)
	{
		$this->children[] = $element;
		return $this;
	}
	
	public function addClass($class)
	{
		$this->class[$class] = $class;
		return $this;
	}
	
	public function removeClass($class)
	{
		$this->class[$class] = null;
		return $this;
	}
	
	public function __toString()
	{
		$return = '<' . $this->tag;
		
		foreach($this->allowedAttributes as $attribute=>$default)
		{
			if(is_int($attribute))
			{
				$attribute = $default;
				$default = null;
			}
			
			$value = $this->{$attribute} ? $this->{$attribute} : $default;
			
			if($value)
			{
				$return .= ' ' . $attribute . '=' . $this->quotes . (is_array($value) ? implode(' ', $value) : $value) . $this->quotes;
			}
		}
		
		if ($this->allowChildren&&(count($this->children)||$this->innerHtml))
		{
			$return .= '>';
			
			if($this->innerHtml)
			{
				$return .= $this->innerHtml;
			}
			
			if(count($this->children)>0)
			{
				foreach($this->children as $child)
				{
					$return .= $child;
				}
			}
			
			$return .= '</' . $this->tag . '>';
		}
		else
		{
			$return .= ' />';
		}

		return $return;
	}
	
	private function constructClass()
	{
		if(is_array($this->class))
		{
			if($this->class!='')
			{
				$class = implode(' ', $this->class);
			}
		}
		else
		{
			$class = $this->class;
		}
		
		return $class;
	}
}

?>